#include "singlegame.h"
#include<QTimer>


singleGame::singleGame()
{
    _level=4;
}
void singleGame::click(int id, int row, int col)
{
    if(!this->_bRedTurn)
        return;

    board::click(id,row,col);

    if(!this->_bRedTurn)
    {
        //启动0.1s定时器，使其刷新完再思考
        QTimer::singleShot(100,this,SLOT(computerMove()));
    /*
        step*_step=getBestMove();
        moveStone(_step->_moveid,_step->_killid,_step->_rowTo,_step->_colTo);
        delete _step;
    */
     }
}

void singleGame::computerMove()
{
    step*_step=getBestMove();
    moveStone(_step->_moveid,_step->_killid,_step->_rowTo,_step->_colTo);
    delete _step;
    update();
}
//
void singleGame::getAllpossibleMove(QVector<step *> &steps)
{
    int min=16,max=32;
    if(this->_bRedTurn)
    {
        min=0,max=16;
    }
    for(int i=min;i<max;++i)
    {
        if(_s[i]._dead)continue;
        for(int row=0;row<=9;++row)
        {
            for(int col=0;col<=8;++col)
            {
                int killid=this->getStoneId(row,col);
                if(sameColor(killid,i))continue;

                if(canMove(i,row,col,killid))
                {
                    saveStep(i,killid,row,col,steps);
                }
            }
        }
    }
}
void singleGame::fakeMove(step*_step)
{
   // _step->killid;
    killStone(_step->_killid);
    moveStone(_step->_moveid,_step->_rowTo,_step->_colTo);
}
void singleGame::unfakeMove(step*_step)
{
    reliveStone(_step->_killid);
    moveStone(_step->_moveid,_step->_rowFrom,_step->_colFrom);
}

//评价局面分
int singleGame::calcScore()
{
    int redTotalScore=0;
    int blakTotalScore=0;
    //动态局面分
    //enum TYPE{JIANG,CHE,PAO,MA,BING,SHI,XIANG};
    static int chessScore[]={1500,100,50,50,20,10,10};
    //黑棋分的总数-红棋分的总数

    for(int i=0;i<16;++i)
    {
        if(_s[i]._dead)continue;
        redTotalScore+=chessScore[_s[i]._type];
    }
    for(int i=16;i<32;++i)
    {
        if(_s[i]._dead)continue;
        blakTotalScore+=chessScore[_s[i]._type];
    }
    return blakTotalScore-redTotalScore;
}

int singleGame::getMaxScore(int level,int culMinScore)
{
    if(level==0)return calcScore();
    //看看有哪些步骤可以走
    QVector<step*>steps;
    getAllpossibleMove(steps);//红棋的可能

    int maxScore=-100000;
    //for(QVector<step*>::iterator it=steps.begin();it!=steps.end();++it)
    while(steps.count())
    {
        step*_step=steps.back(); //*it;
        steps.removeLast();

        fakeMove(_step);
        int score=getMinScore(_level-1,maxScore);//calcScore();
        unfakeMove(_step);
        delete _step;

        if(score>=culMinScore)
        {
            while(steps.count())
            {
                step*_step=steps.back();
                steps.removeLast();
                delete _step;
            }
           return score;
        }
        if(score>maxScore)
        {
            maxScore=score;
        }
        //delete _step;
    }
    return maxScore;
}
//间接递归
int singleGame::getMinScore(int level,int culMaxScore)
{
    if(level==0)return calcScore();
    //看看有哪些步骤可以走
    QVector<step*>steps;
    getAllpossibleMove(steps);//红棋的可能

    int minScore=100000;
    //for(QVector<step*>::iterator it=steps.begin();it!=steps.end();++it)
    while(steps.count())
    {
        step*_step=steps.back(); //*it;
        steps.removeLast();

        fakeMove(_step);
        int score=getMaxScore(_level-1,minScore);//calcScore();
        unfakeMove(_step);   
        delete _step;

        if(score<=culMaxScore)
        {
            while(steps.count())
            {
                step*_step=steps.back();
                steps.removeLast();
                delete _step;
            }
            return score;
        }

        if(score<minScore)
        {
            minScore=score;
        }
        //delete _step;
    }
    return minScore;
}
step* singleGame::getBestMove()
{
/*
1、看看有哪些步骤可以走
2、试着走一下
3、评估走的结果
4、取最好的结果做参考
*/
    //看看有哪些步骤可以走
    QVector<step*>steps;
    getAllpossibleMove(steps);
    //试着走一下
    //评估走的结果
    int maxScore=-100000;
    step*ret=NULL;
    //for(QVector<step*>::iterator it=steps.begin();it!=steps.end();++it)
    while(steps.count())
    {
        step*_step=steps.back();   //*it;
        steps.removeLast();

        fakeMove(_step);//虚假移动
        int score=getMinScore(_level-1,maxScore);
        unfakeMove(_step);

        if(score>maxScore)
        {
            maxScore=score;
            if(ret)delete ret;
            ret=_step;
        }
        else
        {
            delete _step;
        }
    }
    //取最好的结果做参考
    return ret;
}
